package com.tjsinfo.tjsoa.baobiao.action;

import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.baobiao.bo.IHqBBSetManage;
import com.tjsinfo.tjsoa.baobiao.bo.IHqBBYhManage;
import com.tjsinfo.tjsoa.baobiao.vo.THqBBYh;

public class BBYhManageAction extends DispatchAction {
	
	private IHqBBYhManage hqBBYhManage;
	private IHqBBSetManage hqBBSetManage;	

	public IHqBBYhManage getHqBBYhManage() {
		return hqBBYhManage;
	}

	public void setHqBBYhManage(IHqBBYhManage hqBBYhManage) {
		this.hqBBYhManage = hqBBYhManage;
	}	
	
	public IHqBBSetManage getHqBBSetManage() {
		return hqBBSetManage;
	}

	public void setHqBBSetManage(IHqBBSetManage hqBBSetManage) {
		this.hqBBSetManage = hqBBSetManage;
	}

	/**
	 * 获取本报表所有已存在用户信息
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward getYhList(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
			try {
	 			String bbidString = request.getParameter("bbid");
				JSONArray array	 = new JSONArray();
	 			
	 			List<THqBBYh> templist = hqBBYhManage.findAll(Integer.valueOf(bbidString));
		 		for (THqBBYh tempYh: templist ){
		 			JSONObject jsObject = new JSONObject();
		 			jsObject.put("id", tempYh.getId());
		 			jsObject.put("sort", tempYh.getSort());
		 			jsObject.put("dwm", tempYh.getDwmch());
		 			jsObject.put("ryxm", tempYh.getRyxm());		
		 			jsObject.put("rydh", tempYh.getRydh());	
		 			if ("0".equals(tempYh.getState())){
		 				jsObject.put("state", "禁用");	 
		 			}else{
		 				jsObject.put("state", "启用");
		 			}
		 			array.put(jsObject);
		 		}
	 			response.getWriter().print(array);
	 		} catch (IOException e) {
	 			// TODO Auto-generated catch block
	 			e.printStackTrace();
	 		} 
			return null;
		}	
		/**
		 * 准备进入用户列表界面
		 * @param mapping
		 * @param form
		 * @param request
		 * @param response
		 * @return
		 * @throws Exception
		 */
//	public ActionForward toList(ActionMapping mapping, ActionForm form,
//					HttpServletRequest request, HttpServletResponse response)throws Exception {
//				
////		    String deadlineString = hqBBYhManage.getDeadline();
////		    request.setAttribute("month", deadlineString.substring(0,1));
////		    request.setAttribute("day", deadlineString.substring(2,4));
//		    String bbidString = request.getParameter("bbid");
//		    String bbNameString = hqBBSetManage.findByid(Integer.valueOf(bbidString)).getBaobiaoname();
//			request.setAttribute("bbid", bbidString);
//			request.setAttribute("bbname", bbNameString);
//			return mapping.findForward("toList");
//		}		
		/**
			 * 添加用户
			 * @param mapping
			 * @param form
			 * @param request
			 * @param response
			 * @return
			 * @throws Exception
			 */
	public ActionForward addYh(ActionMapping mapping, ActionForm form,
						HttpServletRequest request, HttpServletResponse response)throws Exception {
					
			THqBBYh hqYh=new THqBBYh();
			hqYh.setDwmch(request.getParameter("dwm"));
			hqYh.setRyxm(request.getParameter("ryxm"));
			hqYh.setRydh(request.getParameter("rydh"));
			hqYh.setSort(Integer.valueOf(request.getParameter("sort")));
			String bbidString = request.getParameter("bbid");
			hqYh.setBbid(Integer.valueOf(request.getParameter("bbid")));
			hqYh.setState("1");
			hqBBYhManage.add(hqYh);
			request.setAttribute("linkurl","baobiao/user/userlist.jsp?bbid="+bbidString );
			request.setAttribute("message", "system.save.message");
			return mapping.findForward("success");
	}
	/**
	 * 准备进入修改用户界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateYh(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
			
		    int yhid = Integer.valueOf(request.getParameter("id"));
		    THqBBYh hqYh = hqBBYhManage.findByid(yhid);
		    request.setAttribute("hqyh", hqYh);
			return mapping.findForward("toUpdate");
	}	
	
	/**
	 * 添加用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateYh(ActionMapping mapping, ActionForm form,
					HttpServletRequest request, HttpServletResponse response)throws Exception {
				
		THqBBYh hqYh=new THqBBYh();
		hqYh.setId(Integer .valueOf(request.getParameter("id")));
		hqYh.setDwmch(request.getParameter("dwm"));
		hqYh.setRyxm(request.getParameter("ryxm"));
		hqYh.setRydh(request.getParameter("rydh"));
		String bbidString = request.getParameter("bbid");
		hqYh.setBbid(Integer.valueOf(bbidString));
		hqYh.setSort(Integer.valueOf(request.getParameter("sort")));
		hqYh.setState(request.getParameter("state"));
		hqYh.setRiqi(request.getParameter("riqi"));
		hqBBYhManage.update(hqYh);
		request.setAttribute("linkurl", "baobiao/user/userlist.jsp?bbid="+bbidString);
		request.setAttribute("message", "system.save.message");
		return mapping.findForward("success");
	}
	/**
	 * 准备进入添加用户界面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward enableYh(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response)throws Exception {
		    int yhid = Integer.valueOf(request.getParameter("id"));
		    THqBBYh hqYh = hqBBYhManage.findByid(yhid);
		    if ("0".equals(hqYh.getState())){
		    	hqYh.setState("1");
		    }else{
		    	hqYh.setState("0");
		    }
		    hqBBYhManage.update(hqYh);
		    JSONObject jsObject = new JSONObject();
		    jsObject.put("success",true);
		    response.getWriter().print(jsObject);
			return null;
		}		
}

